package com.itbour.service.com.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.com.UserDao;
import com.itbour.dto.com.CountryInfoDto;
import com.itbour.dto.com.ItbBackFuncInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.com.UserLanguageInfoDto;
import com.itbour.service.com.UserService;

/**
*
* copyright   pirate alliance team 2014
* @author     lunxing
*
*/

public class UserServiceImpl implements UserService {

	private UserDao userDao;

	public List<ItbBackFuncInfoDto> selectUserRoleFunc(Long userId) {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("userId", userId);
		return userDao.selectUserRoleFunc(paramMap);
	}

	public UserInfoDto getUserInfo(String username, String password) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("username", username);
		paramMap.put("password", password);
		paramMap.put("usertype", 0);
		paramMap.put("org_id", 1);// 组织ID 1示界港
		return userDao.selectUserByUserNameAndPassword(paramMap);
	}

	public UserInfoDto userRegister(UserInfoDto userInfoDto) {

		Long id = userDao.selectMaxId();
		if(id == null){
			id = 0l;
		}
		userInfoDto.setUser_id(id + 1);
		int count = userDao.userRegister(userInfoDto);
//
//		GoldenCoinDto goldenCoin = new GoldenCoinDto();
//		goldenCoin.setGolden_coin_id(userInfoDto.getGolden_coin_id());
//		goldenCoin.setAccountbalance(CommonHelper.getGoldenCoinsForReg());
//		coinsDao.insertGoldenCoin(goldenCoin);
//
//		SilverCoinDto silverCoin = new SilverCoinDto();
//		silverCoin.setSilver_coin_id(userInfoDto.getSilver_coin_id());
//		silverCoin.setAccountbalance(CommonHelper.getSilverCoinsForReg());
//		coinsDao.insertSilverCoin(silverCoin);
//
//		if(userLanguageInfo != null && StringUtils.isNotEmpty(userLanguageInfo.getLanguage_id()))
//		{
//			userLanguageInfo.setUser_id(userInfoDto.getUser_id());
//			userDao.insertUserLanguage(userLanguageInfo);
//		}

		if(count == 1)
			return userInfoDto;
		else
			return null;
	}

	public UserInfoDto getServiceUserInfo(String user_id) {
		// TODO Auto-generated method stub
		return userDao.selectUserByUserId(user_id);
	}

	public int updateUserInfo(UserInfoDto userInfo, UserLanguageInfoDto userLanguageInfo)
	{
//		if(userLanguageInfo != null && StringUtils.isNotEmpty(userLanguageInfo.getLanguage_id()))
//		{
//			userLanguageInfo.setUser_id(userInfo.getUser_id());
//			int ret = userDao.updateUserLanguage(userLanguageInfo);
//			if(ret == 0)
//			{
//				userDao.insertUserLanguage(userLanguageInfo);
//			}
//		}
		return userDao.userUpdate(userInfo);
	}
	
	public int updateUserInfo(UserInfoDto userInfo)
	{
		return userDao.userUpdate(userInfo);
	}

	public List<UserInfoDto> getUserListInfo(String user_id){
		return userDao.selectUserListInfoByUserId(user_id);
	}

	public int checkUsernameCnt(String username){
		return userDao.getUsernameCount(username);
	}

	public CountryInfoDto getServiceCountryInfo(String country_id) {
		return userDao.selectCountryByCountryId(country_id);
	}

	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			//selectDataSource(dataSource);
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = userDao.selectCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List data = userDao.selectList(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}




}
